package com.cz.ssyx.acl.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cz.ssyx.acl.service.RoleService;
import com.cz.ssyx.common.result.Result;
import com.cz.ssyx.model.acl.Role;
import com.cz.ssyx.vo.acl.RoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @descriptions:
 * @author: Joker.
 * @date: 2024/4/14 20:46
 * @version: 1.0
 */
@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/admin/acl/role")
//@CrossOrigin    //解决跨域
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation("查询角色列表")
    @GetMapping("/{current}/{limit}")
    public Result listPage(@PathVariable Long current,
                                 @PathVariable Long limit,
                                RoleQueryVo roleQueryVo) {

        //1.创建分页查询对象, 传递当前页和每页记录数
        Page<Role> rolePage = new Page<>(current, limit);

        //2.调用roleService方法实习条件分页查询
        IPage<Role> pageModel =  roleService.selectRolePage(rolePage, roleQueryVo);

        return Result.ok(pageModel);
    }

    @ApiOperation("根据id查询角色")
    @GetMapping("/get/{id}")
    public Result getById(@PathVariable Long id) {
//        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(id != null,Role::getId, id);
//        List<Role> list = roleService.list(queryWrapper);
        Role role = roleService.getById(id);
        return Result.ok(role);
    }

    @ApiOperation("保存角色")
    @PostMapping("/save")
    public Result save(@RequestBody Role role) {
        boolean is_success = roleService.save(role);
        if (is_success){
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }
    }

    @ApiOperation("修改角色")
    @PutMapping("/update")
    public Result update(@RequestBody Role role) {
        roleService.updateById(role);
        return Result.ok(null);
    }

    @ApiOperation("删除角色")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id){
        roleService.removeById(id);
        return Result.ok(null);
    }

    @ApiOperation("批量删除角色")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids){
        roleService.removeByIds(ids);
        return Result.ok(null);
    }


}
